<template>
  <div>
    <div class="container">
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="pluralSort" label="排序字段" align="center"></el-table-column>
        <el-table-column prop="pluralName" label="分区名称" align="center"></el-table-column>
        <el-table-column prop="pluralPic" label="分区图片">
          <template #default="scope">
            <el-image class="table-td-thumb" :src="scope.row.pluralPic" fit="cover"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="gls" label="中奖概率" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="创建分区" v-model="showAddUser" width="65%">
      <el-form ref="form" :model="list" label-width="125px">
        <el-form-item label="分区名称">
          <el-input v-model="from.pluralName"></el-input>
        </el-form-item>
        <el-form-item label="中奖概率">
          <el-input v-model="from.gls"></el-input>
        </el-form-item>
        <el-form-item label="分区图标">
          <div class="crop-demo">
            <img v-if="from.pluralPic" :src="from.pluralPic" class="pre-img" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <input class="crop-input" type="file" name="image" accept="image/*" @change="setImage" />
          </div>
        </el-form-item>
        <el-form-item label="排序字段(值越大越在前)">
          <el-input v-model="from.pluralSort" type="number"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddUser = false">取 消</el-button>
          <el-button v-if="isChange == 1" type="primary" @click="append">确 定</el-button>
          <el-button v-else type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { api, upload } from "@/api/index";
import { formatDate } from "@/common/data";
import moment from "moment";
export default {
  inject: ["reload"],
  filters: {
    formatDate(time) {
      time = time * 1000;
      let date = new Date(time);
      console.log(new Date(time));
      return formatDate(date, "yyyy-MM-dd hh:mm");
    },
  },
  name: "basetable",
  data() {
    return {
      query: {
        pageNo: 1,
      },
      searchName: "",
      tableData: [],
      // multipleSelection: [],
      showAddUser: false,
      pageTotal: 0,
      from: {},
      isChange: "",
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  methods: {
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },

    // 获取分类列表
    getData() {
      api('/web/blindPlural/g', {
        limit: 10,
        offset: this.query.pageNo,
      }).then((res) => {
        this.tableData = res.data.rows;
        this.pageTotal = res.data.total;
      });
    },

    //上传图片
    setImage(file) {
      let imgFile = file.target.files[0];
      let formData = new FormData(); //通过formdata上传
      formData.append("file", imgFile);
      upload(formData).then((res) => {
        this.list.pluralPic = `http://xiuhaoshenghuo.xsiuyang.com/imgs/${res.data[0].fileName}`;
      });
    },

    //显示编辑框
    showBox() {
      this.showAddUser = true;
      this.isChange = 1;
    },

    // 触发搜索按钮
    handleSearch() {
      this.getData();
    },
    // 编辑操作
    handleEdit(index, row) {
      this.showAddUser = true;
      this.isChange = 0;
      this.from = JSON.parse(JSON.stringify(row));
    },
    //修改分类
    edit() {
      this.showAddUser = false;
      api('/web/blindPlural/u', this.from).then((res) => {
        if (res.code === 2000) {
          this.$message.success("修改抢购分区成功");
          this.from = {};
          this.reload();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 分页导航
    handlePageChange(val) {
      this.query.pageNo = val;
      this.getData();
    },
  },
};
</script>

<style lang='scss' scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.crop-demo {
  display: flex;
  align-items: flex-end;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  width: 100px;
  height: 100px;

  .pre-img {
    width: 98px;
    height: 98px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
    object-fit: cover;
  }

  .crop-input {
    position: absolute;
    width: 100px;
    height: 100px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
}
</style>
